package com.tzinfo.tzb.view.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tzinfo.data.PostResultData;
import com.tzinfo.tzb.EdiPostActivity;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.HouseTypeImageVIewerActivity;
import com.yxw.util.YXWDateUtil;

import java.util.List;

/**
 * Created by Administrator on 2014/11/21.
 */
public class PostAdapter2 extends BaseAdapter {

    private Context context;
    private List<PostResultData> data;
    ImageLoader imageLoader;
    PostInterface postInterface;

    public PostAdapter2(Context context, List<PostResultData> data) {
        this.context = context;
        this.data = data;
        imageLoader = ImageLoader.getInstance();
        postInterface = (PostInterface) context;

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.tzb_item_post, parent, false);
            holder.iv_attachment = (ImageView) convertView.findViewById(R.id.item_post_img_attachment);
            holder.iv_avatar = (ImageView) convertView.findViewById(R.id.item_post_avatar);
            holder.tv_subject = (TextView) convertView.findViewById(R.id.item_post_title);
            holder.tv_message = (TextView) convertView.findViewById(R.id.item_post_context);
            holder.tv_username = (TextView) convertView.findViewById(R.id.item_post_username);
            holder.tv_credits = (TextView) convertView.findViewById(R.id.item_post_credits);
            holder.tv_regdate = (TextView) convertView.findViewById(R.id.item_post_regdate);
            holder.tv_dateline = (TextView) convertView.findViewById(R.id.item_post_time);
            holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.item_post_layout_right);
            holder.doll = (LinearLayout) convertView.findViewById(R.id.item_post_adapter_doll);
            holder.tv_screen = (TextView) convertView.findViewById(R.id.item_post_adapter_tv_screen);
            holder.tv_del = (TextView) convertView.findViewById(R.id.item_post_adapter_tv_del);
            holder.tv_edi = (TextView) convertView.findViewById(R.id.item_post_edi);
            holder.iv_attachment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (data.get(position).getAttachment().size() > 0) {
                        Intent i = new Intent(context,HouseTypeImageVIewerActivity.class);
                        i.putExtra("url",data.get(position).getAttachment().get(0).getAttachment());
                        context.startActivity(i);
                    }


                }
            });
            if (position!=1)
                holder.tv_edi.setVisibility(View.GONE);
                holder.tv_edi.setVisibility(View.VISIBLE);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (data.get(position).getAttachment().size() > 0) {
            imageLoader.displayImage(data.get(position).getAttachment().get(0).getAttachment(), holder.iv_attachment);
        }

        holder.tv_message.setText(data.get(position).getMessage().toString());
        holder.tv_subject.setText(data.get(position).getSubject().toString());
        if (data.get(position).getUserinfo() != null) {
            holder.tv_username.setText(data.get(position).getUserinfo().getUsername());
            holder.tv_credits.setText(String.valueOf(data.get(position).getUserinfo().getCredits()));
            holder.tv_regdate.setText(data.get(position).getUserinfo().getRegdate().split(" ")[0]);
            holder.tv_edi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(context)
                            .setItems(new String[]{"删除该贴", "修改该贴"},
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(DialogInterface dialog, int which) {
                                            switch (which) {
                                                case 0:
                                                    postInterface.delete(data.get(position).getFid(), data.get(position).getPid(), "Del", position);
                                                    break;
                                                case 1:
                                                    //新建一个activity，传递pid，fid，Subject，Message
                                                    Intent intent = new Intent(context, EdiPostActivity.class);
                                                    Bundle bundle = new Bundle();
                                                    bundle.putInt("pid", data.get(position).getPid());
                                                    bundle.putInt("fid", data.get(position).getFid());
                                                    bundle.putString("Subject", data.get(position).getSubject());
                                                    bundle.putString("Message", data.get(position).getMessage());
                                                    intent.putExtras(bundle);
                                                    Activity activity = (Activity) context;
                                                    activity.startActivityForResult(intent, 1);

                                                    break;

                                            }
                                            dialog.dismiss();
                                        }
                                    }
                            )
                            .show();
                }
            });
            imageLoader.displayImage(data.get(position).getUserinfo().getAvatar(), holder.iv_avatar);
        }
        holder.tv_dateline.setText(YXWDateUtil.getTimeInterval(data.get(position)
                .getDateline(), "MM/dd/yyyy HH:mm:ss"));
//        final ViewHolder h = holder;
//        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (h.doll.getVisibility() == View.GONE) {
//                    h.doll.setVisibility(View.VISIBLE);
//                } else h.doll.setVisibility(View.GONE);
//            }
//        });
//        holder.tv_screen.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                postInterface.screen(data.get(position).getUserinfo().getUid());
//            }
//        });
        return convertView;
    }

    class ViewHolder {
        ImageView iv_attachment;
        ImageView iv_avatar;
        TextView tv_message;
        TextView tv_subject;
        TextView tv_username;
        TextView tv_credits;
        TextView tv_regdate;
        TextView tv_dateline;
        LinearLayout doll;
        TextView tv_screen;
        TextView tv_del;
        RelativeLayout relativeLayout;
        TextView tv_edi;
    }

    public interface PostInterface {
        public void screen(int Uid);

        public void delete(int a, int b, String s, int position);
    }
}
